package com.jamal2362.apktool.util.as;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ASUtils
{
	public static HashMap<String, String> findIds(File file)
	{
		HashMap<String, String> res = new HashMap<String, String>();
		ArrayList<String> fread = readToArray(file);
		Pattern p = Pattern.compile("type=\"([^\"]+)\"\\s+name=\"(APKTOOL_DUMMY_[^\"]+)\"\\s+id=\"([^\"]+)\"");
		for (String str : fread)
		{
			Matcher m = p.matcher(str);
			if (m.find())
			{
				res.put(m.group(1) + ";" + m.group(2), m.group(3));
			}
		}
		return res;
	}

	public static HashMap<String, String> arrayIds(File file, HashMap<String, String> array)
	{
		ArrayList<String> fread = readToArray(file);
		HashMap<String, String> res = new HashMap<String, String>();
		for (Map.Entry<String, String> entry : array.entrySet())
		{
			String dummy = entry.getKey();
			String id = entry.getValue();
			String dummyName = ASUtils.findById(fread, dummy.split(";")[0], id);
			if (dummyName != null)
				res.put(dummy, dummyName);
		}
		return res;
	}

	public static String findById(ArrayList<String> fread, String type, String id)
	{
		String res = null;
		Pattern p = Pattern.compile("type=\"" + type + "\"\\s+name=\"([^\"]+)\"\\s+id=\"" + id + "\"");
		for (String str : fread)
		{
			Matcher m = p.matcher(str);
			if (m.find())
			{
				res = m.group(1);
				break;
			}
		}
		return res;
	}

	public static String findSplitName(File file)
	{
		String res = null;
		ArrayList<String> fread = readToArray(file);
		Pattern p = Pattern.compile("<meta-data\\s+android:name=\"com\\.android\\.vending\\.splits\"\\s+android:value=\"@xml/(splits\\d+)\"\\s+/>");
		for (String str : fread)
		{
			Matcher m = p.matcher(str);
			if (m.find())
			{
				res = m.group(1);
				break;
			}
		}
		return res;
	}

	public static String replace(String string, String search, String repl)
	{
		if (string == null)
			return "";
		if (search == null || repl == null)
			return string;
		return string.replace(search, repl);
	}

	public static String replacer(String content, String[] duarr, String dummyName)
	{
		if (content == null)
			return "";
		if (duarr == null || duarr.length != 2 || dummyName == null)
			return content;
		final Pattern dummy = Pattern.compile("\\s+<item\\stype=\"" + duarr[0] + "\"\\sname=\"" + duarr[1] + "\">false</item>");
		final Matcher dMatcher = dummy.matcher(content);
		while (dMatcher.find())
		{
			content = dMatcher.replaceAll("");
		}
		content = content.replace("type=\"" + duarr[0] + "\" name=\"" + duarr[1] + '\"', "type=\"" + duarr[0] + "\" name=\"" + dummyName + '\"');
		content = content.replace(duarr[0] + " name=\"" + duarr[1] + '\"', duarr[0] + " name=\"" + dummyName + '\"');
		content = content.replace("\"@" + duarr[0] + "/" + duarr[1] + "\"", "\"@" + duarr[0] + "/" + dummyName + '\"');
		content = content.replace(">@" + duarr[0] + '/' + duarr[1] + '<', ">@" + duarr[0] + '/' + dummyName + '<');
		if (duarr[0].equals("attr"))
		{
			content = content.replace("\"?" + duarr[1] + '\"', "\"?" + dummyName + '\"');
			content = content.replace(">?" + duarr[1] + '<', ">?" + dummyName + '<');
		}
		if (duarr[0].equals("id"))
		{
			content = content.replace('>' + duarr[1] + '<', '>' + dummyName + '<');
		}
		return content;
	}

	public static String replaceReg(String string, String search, String repl)
	{
		if (string == null)
			return "";
		if (search == null || repl == null)
			return string;
		return string.replaceAll(search, repl);
	}

	public static ArrayList<String> readToArray(File file)
	{
		ArrayList<String> trace = new ArrayList<String>();
		String  line;
		try
		{
			FileInputStream fis = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
			while ((line = reader.readLine()) != null)
			{
				trace.add(line);
			}
		}
		catch (Exception e)
		{
			trace.add(e.getMessage());
		}
		return trace;
	}

	public static String readToString(File file)
	{
		String res = "";
		String  line;
		try
		{
			FileInputStream fis = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
			while ((line = reader.readLine()) != null)
			{
				res += line + "\n";
			}
		}
		catch (Exception e)
		{
			res += e.getMessage();
		}
		return res;
	}

	public static String listToString(ArrayList<String> list)
	{
		String res = "";
		for (String str : list)
		{
			res += str + "\n";
		}
		return res;
	}

	public static void writeToFile(File file, String string)
	{
		try
		{
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(string.getBytes("UTF-8"));
			fos.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

